/*     */ package com.feeyin.radar.sys.dao.impl;
/*     */ 
/*     */ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.FinalUtils;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.model.WarningInfo;
import com.feeyin.radar.sys.dao.DataInterfaceDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.DataInterface;
import com.feeyin.radar.sys.model.Users;
/*     */ 
/*     */ public class DataInterfaceDAOImpl extends SqlMapClientDaoSupport
/*     */   implements DataInterfaceDAO
/*     */ {
/*     */   private UserDAO userDAO;
/*     */   private ManageSetDAO manageSetDAO;
/*     */ 
/*     */   public UserDAO getUserDAO()
/*     */   {
/*  41 */     return this.userDAO;
/*     */   }
/*     */ 
/*     */   public void setUserDAO(UserDAO userDAO) {
/*  45 */     this.userDAO = userDAO;
/*     */   }
/*     */ 
/*     */   public ManageSetDAO getManageSetDAO() {
/*  49 */     return this.manageSetDAO;
/*     */   }
/*     */ 
/*     */   public void setManageSetDAO(ManageSetDAO manageSetDAO) {
/*  53 */     this.manageSetDAO = manageSetDAO;
/*     */   }
/*     */ 
/*     */   public DataInterface getDataInterface() {
/*  57 */     List list = new ArrayList();
/*  58 */     list = getSqlMapClientTemplate().queryForList("getDataInterfaces");
/*  59 */     if ((list == null) || (list.size() <= 0)) return null;
/*  60 */     return (DataInterface)list.get(0);
/*     */   }
/*     */ 
/*     */   public boolean insertDataInterface(DataInterface dataInterface) {
/*  64 */     if (dataInterface == null) return false;
/*  65 */     Integer result = (Integer)getSqlMapClientTemplate().insert("insertDataInterface", dataInterface);
/*  66 */     return getDataInterface() != null;
/*     */   }
/*     */ 
/*     */   public boolean updateDataInterface(DataInterface dataInterface)
/*     */   {
/*  71 */     if (dataInterface == null) return false;
/*  72 */     Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateDataInterface", dataInterface));
/*  73 */     return result != null;
/*     */   }
/*     */ 
/*     */   public void insertEMail(WarningInfo warningInfo, int sign)
/*     */   {
/*  78 */     if ((warningInfo != null) && (this.manageSetDAO.isWarningWay("email"))) {
/*  79 */       String month = DateUtils.getStringDateByFormat(warningInfo.getWarningDate(), "MM");
/*  80 */       String day = DateUtils.getStringDateByFormat(warningInfo.getWarningDate(), "dd");
/*  81 */       Integer userId = warningInfo.getSponsorId();
/*  82 */       String warningUserIds = warningInfo.getUserIds();
/*     */ 
/*  84 */       if (sign == 0) {
/*  85 */         String content = month + FinalUtils.MONTH + day + FinalUtils.DAYOFMONTH + FinalUtils.LINK + " " + warningInfo.getForeignName();
/*  86 */         sendEmail(this.userDAO.getUserEmailById(warningInfo.getWarningUserId()), FinalUtils.TIMEREMIND, content);
/*  87 */       } else if (sign == 1) {
/*  88 */         String content = FinalUtils.AGINGCONTRACT + FinalUtils.ID + warningInfo.getForeignCode() + " " + warningInfo.getForeignName();
/*  89 */         sendEmail(this.userDAO.getUserEmailById(warningInfo.getWarningUserId()), FinalUtils.SYSTEMREMIND, content);
/*  90 */       } else if (sign == 2) {
/*  91 */         String content = warningInfo.getForeignName() + FinalUtils.BYMOVE;
/*  92 */         sendEmail(this.userDAO.getUserEmailById(warningInfo.getWarningUserId()), FinalUtils.SYSTEMREMIND, content);
/*  93 */       } else if (sign == 3) {
/*  94 */         String content = warningInfo.getForeignName() + " " + warningInfo.getAgingDay() + FinalUtils.DAY + FinalUtils.AGING;
/*  95 */         sendEmail(this.userDAO.getUserEmailById(warningInfo.getWarningUserId()), FinalUtils.SYSTEMREMIND, content);
/*  96 */       } else if ((sign == 4) && (userId != null) && (!StringUtils.isEmpty(warningUserIds))) {
/*  97 */         List users = new ArrayList();
/*  98 */         users = this.userDAO.getAuditUsers(userId);
/*  99 */         if ((users == null) || (users.size() <= 0)) return;
/* 100 */         String recipients = "";
/* 101 */         String content = FinalUtils.AUDITSALECOST + FinalUtils.ID + warningInfo.getForeignCode();
/* 102 */         for (int i = 0; i < users.size(); i++) {
/* 103 */           Users user = new Users();
/* 104 */           user = (Users)users.get(i);
/* 105 */           if ((warningUserIds.indexOf(user.getId().toString()) != -1) && (userId.compareTo(user.getId()) != 0)) {
/* 106 */             recipients = recipients + this.userDAO.getUserEmailById(user.getId()) + ",";
/*     */           }
/*     */         }
/* 109 */         sendEmail(recipients, FinalUtils.AUDITREMIND, content);
/* 110 */       } else if ((sign == 5) && (userId != null) && (!StringUtils.isEmpty(warningUserIds))) {
/* 111 */         List users = new ArrayList();
/* 112 */         users = this.userDAO.getAuditUsers(userId);
/* 113 */         if ((users == null) || (users.size() <= 0)) return;
/* 114 */         String recipients = "";
/* 115 */         String content = FinalUtils.AUDITSALECOST + FinalUtils.ID + warningInfo.getForeignCode();
/* 116 */         for (int i = 0; i < users.size(); i++) {
/* 117 */           Users user = new Users();
/* 118 */           user = (Users)users.get(i);
/* 119 */           if ((warningUserIds.indexOf(user.getId().toString()) != -1) && (userId.compareTo(user.getId()) != 0)) {
/* 120 */             recipients = recipients + this.userDAO.getUserEmailById(user.getId()) + ",";
/*     */           }
/*     */         }
/* 123 */         sendEmail(recipients, FinalUtils.AUDITREMIND, content);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void sendEmail(String toName, String title, String content)
/*     */   {
/* 135 */     if ((!StringUtils.isEmpty(toName)) && (!StringUtils.isEmpty(title)) && (!StringUtils.isEmpty(content)))
/*     */     {
/* 137 */       DataInterface dataInterface = new DataInterface();
/* 138 */       dataInterface = getDataInterface();
/* 139 */       if ((dataInterface != null) && (!StringUtils.isEmpty(dataInterface.getMailService())) && 
/* 140 */         (!StringUtils.isEmpty(dataInterface.getUserName())) && (!StringUtils.isEmpty(dataInterface.getPassword())))
/*     */       {
/*     */         try
/*     */         {
/* 144 */           Properties props = new Properties();
/* 145 */           props.put("mail.smtp.host", "smtp.163.com");
/* 146 */           props.put("mail.smtp.auth", "true");
/* 147 */           Session s = Session.getInstance(props);
/*     */ 
/* 151 */           MimeMessage message = new MimeMessage(s);
/*     */ 
/* 154 */           InternetAddress from = new InternetAddress(dataInterface.getUserName());
/* 155 */           message.setFrom(from);
/*     */ 
/* 160 */           new InternetAddress(); InternetAddress[] tos = InternetAddress.parse(toName);
/* 161 */           message.setRecipients(Message.RecipientType.TO, tos);
/*     */ 
/* 163 */           message.setSubject(title);
/* 164 */           message.setText(content);
/* 165 */           message.setSentDate(new Date());
/*     */ 
/* 168 */           message.saveChanges();
/* 169 */           Transport transport = s.getTransport("smtp");
/* 170 */           transport.connect(dataInterface.getMailService(), dataInterface.getUserName(), dataInterface.getPassword());
/* 171 */           transport.sendMessage(message, message.getAllRecipients());
/* 172 */           transport.close();
/*     */         }
/*     */         catch (Exception localException)
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\e547415\MyProject\rone\WEB-INF\classes\
 * Qualified Name:     com.feeyin.radar.sys.dao.impl.DataInterfaceDAOImpl
 * JD-Core Version:    0.6.0
 */